<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改用户密码</title>

</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery.form.min.js"></script>
<link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css"/>
<script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script>
<script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
<script type="text/javascript" src="/layer/layer.js"></script>
<script type="text/javascript" src="/js/md5.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>

<body>
<div id="nav-placeholder"></div>
<script>
    $(function () {
        $("#nav-placeholder").load("./top.html");
    });
</script>
<div>
    <form class="form-horizontal" method="post">
        <div class="form-group">
            <label for="password" class="col-sm-1 control-label">原密码</label>
            <div class="col-sm-3">
                <input type="password" class="form-control" id="password" placeholder="原密码">
            </div>
        </div>

        <div class="form-group">
            <label for="newpassword" class="col-sm-1 control-label">新密码</label>
            <div class="col-sm-3">
                <input type="password" class="form-control" id="newpassword" placeholder="新密码">
            </div>
            <button class="btn btn-primary col-sm-offset-1" type="button" onclick="updatePassword()">确认修改</button>
        </div>


    </form>
</div>

</body>

<script>


    $(function () {
        getUser();
    })

    function getUser() {
        $.ajax({
            url: "/getUser",
            type: "GET",
            success: function (data) {
                let user = data.data;
                if (data.code == 0) {
                    $("#username").text(user.userName);
                    $("#img1").attr("src", user.head);
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg("客户端请求有误");
            }
        })
    }

    function updatePassword() {
        let password = $("#password").val();
        let newpassword = $("#newpassword").val();
        let salt = g_passsword_salt;
        let str1 = salt.charAt(0) + salt.charAt(2) + salt.charAt(4) + password;
        let str2 = salt.charAt(0) + salt.charAt(2) + salt.charAt(4) + newpassword;
        let formPassword1 = md5(str1);
        let formPassword2 = md5(str2);
        console.log(formPassword1);
        console.log(formPassword2);
        $.ajax({
            url: "/user/update/password",
            type: "POST",
            data: {
                password: formPassword1,
                newpassword: formPassword2,
            },
            success: function (data) {
                console.log(data);
                if (data.code == 0) {
                    layer.msg("密码修改成功");
                    $("#password").val("");
                    $("#newpassword").val("");
                } else {
                    layer.msg(data.msg);
                }
            }
            ,
            error: function (data) {
            }
        });
    }

</script>
</html>